package com.rms.hkpc.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.rms.hkpc.MyApplication;
import com.rms.hkpc.R;

import static android.app.Notification.VISIBILITY_SECRET;


public class NotificationUtil {
    private static final String CHANNEL_ID = "channel_id_vibration";
    private static final String CHANNEL_ID_BACK = "channel_id_un_vibration";
    private static final String CHANNEL_NAME = MyApplication.getContext().getResources().getString(R.string.work_order_notify);
    private NotificationManager manager;
    private static volatile NotificationUtil instance;

    public static NotificationUtil getInstance() {
        if (instance == null) {
            synchronized (NotificationUtil.class) {
                if (instance == null) {
                    instance = new NotificationUtil();
                }
            }
        }
        return instance;
    }

    public NotificationManager getNotificationManager() {
        if (manager == null) {
            manager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public NotificationChannel getChannel(boolean isVibration) {
        NotificationChannel channel;
        if (isVibration) {
            channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setLockscreenVisibility(VISIBILITY_SECRET); // 锁屏显示通知
            channel.setBypassDnd(true); // 绕过请勿打扰模式
            channel.setShowBadge(true); // 桌面launcher消息角标
            channel.setLightColor(Color.RED); // 闪光时的灯光颜色
            channel.enableLights(true); // 闪光
            channel.setVibrationPattern(new long[]{100, 100, 200}); // 振动频率
            channel.enableVibration(true); // 振动
        }else {
            channel = new NotificationChannel(CHANNEL_ID_BACK, CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN);
            channel.setSound(null,null);
            channel.setVibrationPattern(new long[]{0}); // 振动频率
            channel.enableVibration(false); // 振动
        }
        return channel;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getNotification(Context context,boolean isVibration) {
        return new Notification.Builder(context, isVibration?CHANNEL_ID:CHANNEL_ID_BACK)
                .setOnlyAlertOnce(true);
    }

    public NotificationCompat.Builder getNotificationCompat(Context context,boolean type) {
        return new NotificationCompat.Builder(context, type?CHANNEL_ID:CHANNEL_ID_BACK)
                .setOnlyAlertOnce(true);
    }
}
